﻿using System;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient.IntegrationTests
{
	[TestClass]
	public class CloudBlobClientIntegrationTests
	{
		ICloudBlobClient cloudBlobClient;

		[TestInitialize]
		public void Create_CloudBlobClient()
		{
			ICloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount.Wrap();
			cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

			Assert.IsNotNull(cloudBlobClient);
		}

		[TestMethod]
		public void Upload_download_blob()
		{
			string blobName = "fooblob";
			string containerName = "sometesetcontainer";
			string blobContent = "some text";
			string agencyName = "Hittepåmäklaren";
			string agencyNameEncoded = agencyName.ToStorageMetadataSafeValue();
			string agencyMetadataKey = "Agency";

			CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
			cloudBlobContainer.CreateIfNotExist();

			CloudBlob cloudBlobUp = cloudBlobContainer.GetBlobReference(blobName);

			cloudBlobUp.Metadata.Add(agencyMetadataKey, agencyNameEncoded);
			cloudBlobUp.UploadText(blobContent);

			CloudBlob cloudBlobDown = cloudBlobContainer.GetBlobReference(blobName);

			cloudBlobDown.FetchAttributes();

			Assert.AreEqual(agencyName, cloudBlobDown.Metadata[agencyMetadataKey].FromStorageMetadataSafeValue());

			string result = cloudBlobDown.DownloadText();

			Assert.AreEqual(blobContent, result);

			cloudBlobContainer.Delete();
		}

		[TestMethod]
		public void ListBlobs()
		{
			string blobName = "fooblob";
			string containerName = "sometesetcontainer";
			string blobContent = "some text";

			CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
			cloudBlobContainer.CreateIfNotExist();

			CloudBlob cloudBlobUp = cloudBlobContainer.GetBlobReference(blobName);
			cloudBlobUp.UploadText(blobContent);

			var lastModifiedUtc = cloudBlobUp.Properties.LastModifiedUtc;

			var tenMinutesAgo = lastModifiedUtc.Subtract(TimeSpan.FromMinutes(10));
			var oneBlobModifiedSinceTenMinutesAgo = cloudBlobContainer.ListBlobs(new BlobRequestOptions
				{
					/* Please notice that .ListBlobs does not support AccessCondition: http://msdn.microsoft.com/en-us/library/dd179371.aspx
					   This does not work! */
					AccessCondition = AccessCondition.IfModifiedSince(tenMinutesAgo)
				}).ToArray();

			//Thread.Sleep(TimeSpan.FromSeconds(2));
			//Thread.Sleep(TimeSpan.FromSeconds(10));

			var now = DateTime.UtcNow;

			var noBlobsModifiedSinceNow = cloudBlobContainer.ListBlobs(new BlobRequestOptions
				{
					AccessCondition = AccessCondition.IfModifiedSince(now)
				}).ToArray();

			//Assert.IsTrue(oneBlobModifiedSinceTenMinutesAgo.Any(), "Expected one blob to be modified since ten minutes ago.");
			//Assert.IsFalse(noBlobsModifiedSinceNow.Any(), "Expected no blob to be modified since now.");

			cloudBlobContainer.Delete();
		}
	}
}